<?php

namespace App\Http\Controllers\Admin;

use App\Models\Tag;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class TagController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function customerIndex()
    {
        return view('admin.tag.customer_index');
    }

    public function customerData(Request $request)
    {
        $search = [];
        $search['customer_name'] = $request->customer_name?:'';
        $search['jin_customer_code'] = $request->jin_customer_code?:'';

        $now_page=intval($request->input('page'));
        $per_page_num=intval($request->input('limit'));
        //偏移量
        $offset=($now_page-1)*$per_page_num;
        if($offset<0) $offset=0;

        $tagModel = new Tag();
        $where = [];
        $res =  $tagModel->getCustomerList($where,$search,$offset,$per_page_num);

        $data = ['code' => 0,'msg' => '正在请求中...','count' => $res['count'],'data' => $res['data']];
        return response()->json($data);
    }


    public function customerEdit(Request $request)
    {
        $id = $request->id;
        $tagModel = new Tag();
        $data = $tagModel->getOneCustomerData('v_customer_field',['id'=>$id]);
        return view('admin.tag.customer_edit',['customer'=>$data]);

    }

    public function customerUpdate(Request $request)
    {
        $id = $request->id;
        $data = [
            'jin_project_name' => '客户',
            'jin_customer_name' => $request->jin_customer_name,
            'jin_customer_code' => $request->jin_customer_code,
            'yao_supplier_code' => $request->yao_supplier_code,
            'type' => $request->type,
      ];
        $res = DB::table('v_customer_field')->where('id',$id)->update($data);
        if ($res){
            return response()->json(['code'=>0,'msg'=>'修改成功']);
        }else{
            return response()->json(['code'=>401,'msg'=>'修改失败']);
        }
    }


    public function customerAdd(Request $request)
    {
        return view('admin.tag.customer_add');
    }

    public function customerSave(Request $request)
    {
        $data = [
            'jin_project_name' => '客户',
            'jin_customer_name' => $request->jin_customer_name,
            'jin_customer_code' => $request->jin_customer_code,
            'yao_supplier_code' => $request->yao_supplier_code,
            'type' => $request->type,
        ];

        $res = DB::table('v_customer_field')->insert($data);
        if ($res){
            return response()->json(['code'=>0,'msg'=>'添加成功']);
        }else{
            return response()->json(['code'=>401,'msg'=>'添加失败']);
        }
    }

    /** 供应商管理
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */

    public function supplierIndex()
    {
        return view('admin.tag.supplier_index');

    }

    public function supplierData(Request $request)
    {
        $search = [];
        $search['customer_name'] = $request->customer_name?:'';
        $search['jin_supplier_code'] = $request->jin_supplier_code?:'';

        $now_page=intval($request->input('page'));
        $per_page_num=intval($request->input('limit'));
        //偏移量
        $offset=($now_page-1)*$per_page_num;
        if($offset<0) $offset=0;

        $tagModel = new Tag();
        $where = [];
        $res =  $tagModel->getSupplierList($where,$search,$offset,$per_page_num);

        $data = ['code' => 0,'msg' => '正在请求中...','count' => $res['count'],'data' => $res['data']];
        return response()->json($data);
    }


    public function SupplierEdit(Request $request)
    {
        $id = $request->id;
        $tagModel = new Tag();
        $data = $tagModel->getOneCustomerData('v_supplier_field',['id'=>$id]);
        return view('admin.tag.supplier_edit',['customer'=>$data]);

    }


    public function supplierUpdate(Request $request)
    {
        $id = $request->id;
        $data = [
            'jin_supplier_name' => $request->jin_supplier_name,
            'jin_supplier_code' => $request->jin_supplier_code,
            'yao_supplier_code' => $request->yao_supplier_code,
        ];

        $res = DB::table('v_supplier_field')->where('id',$id)->update($data);
        if ($res){
            return response()->json(['code'=>0,'msg'=>'修改成功']);
        }else{
            return response()->json(['code'=>401,'msg'=>'修改失败']);
        }
    }

    public function supplierAdd(Request $request)
    {
        return view('admin.tag.supplier_add');
    }

    public function supplierSave(Request $request)
    {
        $data = [
            'jin_project_name' => '供应商',
            'jin_supplier_name' => $request->jin_supplier_name,
            'jin_supplier_code' => $request->jin_supplier_code,
            'yao_supplier_code' => $request->yao_supplier_code,
        ];

        $res = DB::table('v_supplier_field')->insert($data);
        if ($res){
            return response()->json(['code'=>0,'msg'=>'添加成功']);
        }else{
            return response()->json(['code'=>401,'msg'=>'添加失败']);
        }
    }
}
